package com.ruoyi.event.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.event.domain.EventLeave;
import com.ruoyi.event.domain.bo.EventLeaveBo;
import com.ruoyi.event.domain.vo.EventLeaveVo;
import com.ruoyi.event.mapper.EventLeaveMapper;
import com.ruoyi.event.service.IEventLeaveService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 请假Service业务层处理
 *
 * @author strongme
 * @date 2023-02-04
 */
@RequiredArgsConstructor
@Service
public class EventLeaveServiceImpl implements IEventLeaveService {

    private final EventLeaveMapper baseMapper;

    /**
     * 查询请假
     */
    @Override
    public EventLeaveVo queryById(Long leaveId) {
        return baseMapper.selectVoById(leaveId);
    }

    /**
     * 查询请假列表
     */
    @Override
    public TableDataInfo<EventLeaveVo> queryPageList(EventLeaveBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<EventLeave> lqw = buildQueryWrapper(bo);
        Page<EventLeaveVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询请假列表
     */
    @Override
    public List<EventLeaveVo> queryList(EventLeaveBo bo) {
        LambdaQueryWrapper<EventLeave> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<EventLeave> buildQueryWrapper(EventLeaveBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<EventLeave> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getUserId() != null, EventLeave::getUserId, bo.getUserId());
        lqw.eq(StringUtils.isNotBlank(bo.getWcd()), EventLeave::getWcd, bo.getWcd());
        lqw.eq(StringUtils.isNotBlank(bo.getBrlxdh()), EventLeave::getBrlxdh, bo.getBrlxdh());
        lqw.eq(StringUtils.isNotBlank(bo.getJrlxdh()), EventLeave::getJrlxdh, bo.getJrlxdh());
        lqw.eq(bo.getQsrq() != null, EventLeave::getQsrq, bo.getQsrq());
        lqw.eq(bo.getJsrq() != null, EventLeave::getJsrq, bo.getJsrq());
        lqw.eq(StringUtils.isNotBlank(bo.getDbr()), EventLeave::getDbr, bo.getDbr());
        lqw.eq(StringUtils.isNotBlank(bo.getDbrdh()), EventLeave::getDbrdh, bo.getDbrdh());
        lqw.eq(StringUtils.isNotBlank(bo.getQjsy()), EventLeave::getQjsy, bo.getQjsy());
        lqw.eq(StringUtils.isNotBlank(bo.getImg()), EventLeave::getImg, bo.getImg());
        lqw.eq(StringUtils.isNotBlank(bo.getStatus()), EventLeave::getStatus, bo.getStatus());
        lqw.eq(bo.getSpr() != null, EventLeave::getSpr, bo.getSpr());
        lqw.eq(StringUtils.isNotBlank(bo.getSpdw()), EventLeave::getSpdw, bo.getSpdw());
        return lqw;
    }

    /**
     * 新增请假
     */
    @Override
    public Boolean insertByBo(EventLeaveBo bo) {
        EventLeave add = BeanUtil.toBean(bo, EventLeave.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setLeaveId(add.getLeaveId());
        }
        return flag;
    }

    /**
     * 修改请假
     */
    @Override
    public Boolean updateByBo(EventLeaveBo bo) {
        EventLeave update = BeanUtil.toBean(bo, EventLeave.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(EventLeave entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除请假
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
